"""定时发文助手"""
import os
import logging
import platform
import time
import datetime
from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.triggers.cron import CronTrigger
# from package: 包名称，通常是一个目录
# module: 模块名称 通常是一个 Python 文件
from auto.sleep.data import go_to_sleep
from auto.sleep.data import get_pitcute
from auto.sleep.push import auto_write_csdn
from auto.sleep.push import atuo_write_douyu
from auto.sleep.push import auto_write_weibo
from auto.sleep.push import auto_wirte_xiaohongsh
#from auto.sleep.push import auto_write_kuaisou
from auto.sleep.push import auto_write_toutiao
from auto.sleep.push import auto_wirte_maimai
from auto.sleep.push import auto_write_baijia
from auto.sleep.push import auto_wirte_dedao
from auto.sleep.push import auto_wirte_jike
LOG_FORMAT = "[%(asctime)s][%(levelname)s][%(filename)s:%(funcName)s:%(lineno)d] %(message)s"
DATE_FORMAT = '%Y-%m-%d %H:%M:%S'


#######################记录每日反思###################################

def future_for_sleep(msg):
    """
      每天写50字产品理解输出，10行代码
    """
    if msg is None or len(msg) == 0:
        habit_detail = go_to_sleep.interface_go_to_sleep()
    else:
        habit_detail = msg
    random_images = get_pitcute.interface_get_three_pic(4)
    habit_name = "21点远离手机提醒"
    # 格式化为 年-月-日
    formatted_date = datetime.datetime.now().strftime('%Y-%m-%d')
    habit_name = formatted_date + " " + habit_name

    try:
        # csdn 
        try:
            time.sleep(3)
            auto_wirte_maimai.interface_auo_upload_msg_maimai(random_images,habit_name,habit_detail)
            #auto_write_csdn.interface_auo_upload_mycsdn(random_images,habit_name,habit_detail)
            sleep(1)
        except Exception as err:
            print(f"Unexpected {err=}, {type(err)=}")
        try:
            time.sleep(3)
            # 斗鱼
            atuo_write_douyu.interface_auto_post_msg_douyu(random_images, habit_name, habit_detail)
        except Exception as err:
            print(f"Unexpected {err=}, {type(err)=}")
        try:
            time.sleep(3)
            # 微博
            auto_write_weibo.interface_auo_upload_msg_weibo(random_images, habit_name, habit_detail)
        except Exception as err:
            print(f"Unexpected {err=}, {type(err)=}")
        try:
            time.sleep(3)
            # 小红书
            #auto_wirte_xiaohongsh.interface_auo_upload_msg_xiaohongshu(random_images,habit_name,habit_detail)
        except Exception as err:
            print(f"Unexpected {err=}, {type(err)=}")
        # 快手
        try:
            time.sleep(3)
            auto_write_kuaisou.interface_auo_upload_msg_kuaishou(random_images,habit_name,habit_detail)
        except Exception as err:
            print(f"Unexpected {err=}, {type(err)=}")
        try:
            # 头条
            time.sleep(3)
            auto_write_toutiao.interface_auo_upload_weitoutiao(random_images,habit_name,habit_detail)
        except Exception as err:
            print(f"Unexpected {err=}, {type(err)=}")
        
        try:
            time.sleep(3)
            auto_wirte_dedao.interface_auo_upload_msg_dedao(random_images,habit_name,habit_detail)
        except Exception as err:
            print(f"Unexpected {err=}, {type(err)=}")
        try:
            time.sleep(3)
            auto_wirte_jike.interface_auo_upload_msg_jike(random_images,habit_name,habit_detail)
        except Exception as err:
           print(f"Unexpected {err=}, {type(err)=}")
        try:
            time.sleep(3)
            #auto_wirte_jike.interface_auo_upload_msg_jike(random_images,habit_name,habit_detail)
        except Exception as err:
           print(f"Unexpected {err=}, {type(err)=}")
        
        try:
            time.sleep(3)
            auto_write_baijia.interface_auo_upload_msg_baijia(random_images,habit_name,habit_detail)
        except Exception as err:
           print(f"Unexpected {err=}, {type(err)=}")

        
    except Exception as err:
        print(f"Unexpected {err=}, {type(err)=}")
###########################################################
if __name__ == "__main__":
    if platform.system() == "Windows":
        LOG_PATH = r"D:\mp4\log\sleep.log"
    else:
        LOG_PATH = "write.log"
    logging.basicConfig(level=logging.DEBUG,
                        format=LOG_FORMAT,
                        datefmt=DATE_FORMAT,
                        filename=LOG_PATH
                        )

    logging.info("""
        ┌──────────────────────────────────────────────────────────────────────┐
        │                                                                      │    
        │                      •  Start pythonTryEverythingWin  •                             │
        │                                                                      │
        └──────────────────────────────────────────────────────────────────────┘
    """)

    job_defaults = {
        'coalesce': False,
        'max_instances': 1
    }
    future_for_sleep()
    # 调度器组件
    backsched = BlockingScheduler(job_defaults=job_defaults, timezone='Asia/Shanghai')
    # future_for_sleep callable函数 习惯养成--早睡早起
    # crontab 是 Linux 系统中用于设置周期性被执行的指令的命令
    # 0 0 * * *	每天的午夜（0 点）执行一次
    backsched.add_job(future_for_sleep, CronTrigger.from_crontab("0 22 * * *"), id="get_up")
    backsched.start()
    